﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using HalconDotNet;

namespace ImageControl
{
    /// <summary>
    /// ROI类型
    /// </summary>
    public enum DrawingType
    {
        [Description("矩形")]
        Rectangle1,
        [Description("带角度矩形")]
        Rectangle2,
        [Description("圆")]
        Circle,
        [Description("椭圆")]
        Ellipse,
        [Description("圆弧")]
        Arc,
    }


    public class DrawingItem
    {
        public DrawingItem(string name, string color, DrawingType drawingType, HDrawingObject hDrawingObject)
        {
            Name = name;
            Color = color;
            DrawingType = drawingType;
            HDrawingObject = hDrawingObject;
        }

        public DrawingItem(string name, DrawingType drawingType, HDrawingObject hDrawingObject)
        {
            Name = name;
            Color = "green";
            DrawingType = drawingType;
            HDrawingObject = hDrawingObject;
        }

        public string Name { get; set; }
        public string Color { get; set; } = "green";
        public DrawingType DrawingType { get; set; } = DrawingType.Rectangle1;

        public HDrawingObject HDrawingObject { get; set; }

        public HObject GetRegion()
        {
            return HDrawingObject.GetDrawingObjectIconic();
        }

        public void Save(XmlElement node)
        {
            node.SetAttribute("Name", Name);
            node.SetAttribute("Color", Color);
            node.SetAttribute("DrawingType", DrawingType.ToString());
            HTuple hv_result = new HTuple();
            hv_result.Dispose();
            switch (DrawingType)
            {
                case DrawingType.Rectangle1:
                    hv_result = HDrawingObject.GetDrawingObjectParams(new HTuple("color", "row1", "column1", "row2", "column2"));
                    break;
                case DrawingType.Rectangle2:
                    hv_result = HDrawingObject.GetDrawingObjectParams(new HTuple("color", "row1", "column1", "row2", "column2"));
                    break;
                case DrawingType.Circle:
                    hv_result = HDrawingObject.GetDrawingObjectParams(new HTuple("color", "row1", "column1", "row2", "column2"));
                    break;
                case DrawingType.Ellipse:
                    hv_result = HDrawingObject.GetDrawingObjectParams(new HTuple("color", "row1", "column1", "row2", "column2"));
                    break;
                case DrawingType.Arc:
                    hv_result = HDrawingObject.GetDrawingObjectParams(new HTuple("color", "row1", "column1", "row2", "column2"));
                    break;
                default:
                    break;
            }
            string obj = string.Join(",", hv_result);
            node.SetAttribute("HDrawingObject", obj);
        }

        public void Load(XmlElement node)
        {
            Name = node.Attributes["Name"].Value;
            Color = node.Attributes["Color"].Value;
            string val;
            val = node.GetAttribute("DrawingType");
            if (val != "")
            {
                this.DrawingType = (DrawingType)Enum.Parse(typeof(DrawingType), val);
            }
            val = node.GetAttribute("HDrawingObject");
            string[] obj = val.Split(',');
            switch (DrawingType)
            {
                case DrawingType.Rectangle1:
                    HDrawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, double.Parse(obj[1]), double.Parse(obj[2]), double.Parse(obj[3]), double.Parse(obj[4]));

                    break;
                case DrawingType.Rectangle2:
                    HDrawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE2, double.Parse(obj[1]), double.Parse(obj[2]), double.Parse(obj[3]), double.Parse(obj[4]), double.Parse(obj[5]));
                    break;
                case DrawingType.Circle:
                    HDrawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, double.Parse(obj[1]), double.Parse(obj[2]), double.Parse(obj[3]));
                    break;
                case DrawingType.Ellipse:
                    HDrawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.ELLIPSE, double.Parse(obj[1]), double.Parse(obj[2]), double.Parse(obj[3]), double.Parse(obj[4]), double.Parse(obj[5]));
                    break;
                case DrawingType.Arc:
                    HDrawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE_SECTOR, double.Parse(obj[1]), double.Parse(obj[2]), double.Parse(obj[3]), double.Parse(obj[4]), double.Parse(obj[5]));
                    break;
            }
            HDrawingObject.SetDrawingObjectParams("color", obj[0]);
        }
    }
}
